import Mock from 'mockjs';


const data = Mock.mock({
	'list|10': [{
		title: '@ctitle',
		id: '@id',
		url: '@image(50x50, @color)',
		desc: "@cword(20, 30)"
	}]
})


Mock.mock('/api/list', () => {
	return {
		code: 200,
		data: data.list
	}
})

// 删除接口
Mock.mock('/api/list/delete', (req) => {
	// 获取删除id
	const { id } = JSON.parse(req.body)
	// 过滤id
	data.list = data.list.filter(v => v.id !== id)
	// 返回状态
	return {
		code: 200,
		mess: '删除成功'
	}
})

// 编辑接口
Mock.mock('/api/list/edit', (req) => {
	// 获取 修改的数据对象
	const modifyItem = JSON.parse(req.body)
	// 获取 数组中的对象
	const item = data.list.find(v => v.id === modifyItem.id)
	console.log(modifyItem, item);
	// 修改
	Object.assign(item, modifyItem)
	// 返回状态
	return {
		code: 200,
		mess: '修改成功'
	}
})